package com.tang.shardingjdbcdemo.utils;/*
 *   @author Dram
 *   @create 2021-12-24 13:21
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Dream
 */
public class DateUtils {
    /**
     * 得到当前日期和当前日期之后的所有日期 （字符串）
     *
     * @param day
     * @param pattern
     * @return
     */
    public List<String> getDateStringAfterDay(int day, String pattern) {
        return getDateStringAfter(new Date(), day, pattern);
    }

    public List<String> getDateStringAfterDay(int day) {
        return getDateStringAfter(new Date(), day, "yyyy-MM-dd");
    }

    /**
     * 得到当前日期和当前日期之后的所有日期
     *
     * @param day
     * @return
     */
    public List<Date> getDateAfterDay(int day) {
        Date dateBefore = getDateAfter(new Date(), day);
        return getDateWithRange(new Date(), dateBefore);
    }


    /**
     * 得到当前日期和当前日期之后的所有日期 （字符串）
     *
     * @param day
     * @param pattern
     * @return
     */
    public List<String> getDateStringBeforeDay(int day, String pattern) {
        return getDateStringBefore(new Date(), day, pattern);
    }

    public List<String> getDateStringBeforeDay(int day) {
        return getDateStringBefore(new Date(), day, "yyyy-MM-dd");
    }

    /**
     * 得到当前日期和当前日期之后的所有日期
     *
     * @param day
     * @return
     */
    public List<Date> getDateBeforeDay(int day) {
        Date dateBefore = getDateBefore(new Date(), day);
        return getDateWithRange(dateBefore, new Date());
    }

    /**
     * 得到时间范围之内的所有日期
     *
     * @param begin
     * @param
     * @return
     */
    public List<String> getDateStringAfter(Date begin, int day, String pattern) {
        return
                this.getDateWithRange(begin, getDateAfter(begin, day)).stream().map((item) -> {
                    return this.dateToString(item, pattern);
                }).collect(Collectors.toList());
    }

    /**
     * @param begin
     * @param
     * @return
     */
    public List<String> getDateStringBefore(Date begin, int day, String pattern) {
        return
                this.getDateWithRange(getDateBefore(begin, day), begin).stream().map((item) -> {
                    return this.dateToString(item, pattern);
                }).collect(Collectors.toList());
    }

    /**
     * 得到时间范围之内的所有日期
     *
     * @param begin
     * @param end
     * @return
     */
    public List<String> getDateStringWithRange(Date begin, Date end, String pattern) {
        return
                this.getDateWithRange(begin, end).stream().map((item) -> {
                    return this.dateToString(item, pattern);
                }).collect(Collectors.toList());
    }

    /**
     * 得到时间范围之内的所有日期
     *
     * @param begin
     * @param end
     * @return
     */
    public List<String> getDateStringWithRange(String begin, String end, String pattern) {
        return
                this.getDateWithRange(stringToDate(begin, pattern), stringToDate(end, pattern)).stream().map((item) -> {
                    return this.dateToString(item, pattern);
                }).collect(Collectors.toList());
    }

    /**
     * 得到时间范围之内的所有日期
     *
     * @param begin
     * @param end
     * @return
     */
    public List<Date> getDateWithRange(Date begin, Date end) {
        List<Date> list = new ArrayList<>();
        list.add(begin);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(begin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(end);
        while (end.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            list.add(calBegin.getTime());
        }
        return list;
    }

    /**
     * 得到几天前的日期
     *
     * @param date
     * @param day
     * @return
     */
    public Date getDateBefore(Date date, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 得到几天前的日期 并且转为字符串
     *
     * @param date
     * @param day
     * @return
     */
    public String getDateStringBefore(Date date, int day) {
        Date dateAfter = this.getDateBefore(date, day);
        return this.dateToString(dateAfter, "yyyy-MM-dd");
    }


    /**
     * 得到几天后的日期
     *
     * @param date
     * @param day
     * @return
     */
    public Date getDateAfter(Date date, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 得到几天后的日期 并且转为字符串
     *
     * @param date
     * @param day
     * @return
     */
    public String getDateStringAfter(Date date, int day) {
        Date dateAfter = this.getDateAfter(date, day);
        return this.dateToString(dateAfter, "yyyy-MM-dd");
    }

    public String getDateStringAfter(int day) {
        Date dateAfter = this.getDateAfter(new Date(), day);
        return this.dateToString(dateAfter, "yyyy-MM-dd");
    }

    /**
     * 字符串转化为日期格式
     *
     * @param date
     * @param pattern
     * @return
     */
    public Date stringToDate(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date parse = format.parse(date);
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 时间转化为字符串
     *
     * @param date
     */
    public String dateToString(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String ans = format.format(date);
        return ans;
    }

    /**
     * 去除中间的 -
     *
     * @param date
     * @return
     */
    public String removeLine(String date) {
        String[] split = date.split("-");
        String ans = "";
        for (String s : split) {
            ans += s;
        }
        return ans;
    }

    public List<String> removeLineList(List<String> date) {
        return date.stream().map((item) -> {
            return removeLine(item);
        }).collect(Collectors.toList());
    }

    public static void main(String[] args) throws ParseException {
        /*DateUtils test = new DateUtils();
        //yyyy-MM-dd HH:mm:ss

        System.out.println("当前日期前后 X 天的日期");
        System.out.println(test.getDateStringAfter(new Date(), 3));
        System.out.println(test.getDateStringBefore(new Date(), 24));
        System.out.println("去除日期中间的 - ");
        System.out.println(test.removeLine(test.getDateStringAfter(3)));


        System.out.println("指定日期范围之间的所有日期");
        System.out.println(test.getDateStringWithRange("2020-09-01", "2020-09-03", "yyyy-MM-dd"));
        System.out.println(test.getDateStringWithRange(new Date(), test.getDateAfter(new Date(), 4), "yyyy-MM-dd"));

        System.out.println("指定日期之后X 天的日期");
        System.out.println(test.getDateStringAfter(new Date(), 3, "yyyy-MM-dd"));
        System.out.println(test.getDateStringAfterDay(3, "yyyy-MM-dd"));

        System.out.println("指定日期之前X 天的日期");
        System.out.println(test.getDateStringBefore(new Date(), 3, "yyyy-MM-dd"));
        System.out.println(test.getDateStringBeforeDay(3));
        System.out.println(test.getDateStringBeforeDay(3, "yyyy-MM-dd"));

        System.out.println("当前日期和指定 X 天之前的所有日期");
        System.out.println(test.getDateBeforeDay(3));
        System.out.println(test.getDateStringBeforeDay(3));

        System.out.println("当前日期和指定 X 天之后的所有日期");
        System.out.println(test.getDateAfterDay(3));
        System.out.println(test.getDateStringAfterDay(3));


        System.out.println("去除日期中间的 - (列表)");
        System.out.println(test.removeLineList(test.getDateStringAfterDay(3)));*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = simpleDateFormat.parse("2021-12-24 11:18:00");
        Date end = simpleDateFormat.parse("2022-01-24 11:18:20");
        DateUtils test = new DateUtils();
        List<String> yyyyMMdd = test.getDateStringWithRange(start, end, "yyyyMMdd");
        System.out.println(yyyyMMdd);


    }
}
